﻿//======================================================================
//
//        Copyright (C) 2008 HOHO``
//        All rights reserved
//
//        filename :$safeitemrootname$
//        description :除Login页面以外所有页面的父类。控制和判断权限访问。
//
//        created by 裘中杰 at  $time$
//======================================================================


using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Xml.Linq;
using Hisun.Controllers;
using Hisun.Models;
using System.Collections.Generic;

namespace Hisun.Models
{
    public class AbcPageBase : System.Web.UI.Page
    {
        private HisunDBDataContext db = new HisunDBDataContext();

        public override string StyleSheetTheme
        {
            get//样式表
            {
                return Session[ServerVars.sysSessionSkinID] == null ? string.Empty : Session[ServerVars.sysSessionSkinID].ToString();
            }
        }


        protected void Page_PreInit(object sender, EventArgs e)
        {
            string requestFile = Request.ServerVariables["PATH_INFO"].ToString().ToLower();
            var menu = GetMenu(requestFile);

            if (Session[ServerVars.sysSessionUserAuths] == null)
            {
                Response.Write(string.Format(@"<meta http-equiv=""refresh"" target=""_top"" content=""0;url=/Login.aspx?redir={0}"">", requestFile));
                Response.End();
            }

            var b = ((List<int>)Session[ServerVars.sysSessionUserAuths]).Contains(menu.权限ID);
            if (!b && menu.权限ID != 0)
            {
                throw new Exception("无权访问!");
            }
        }

        private TAbc_JS_Menu GetMenu(string pathfile)
        {
            if (db.TAbc_JS_Menus.Count(m => m.页面文件 == pathfile) == 0)
            {
                throw new Exception("菜单数据库中没有对应的权限!");
            }
            var menu = (from m in db.TAbc_JS_Menus
                        where m.页面文件 == pathfile
                        select m).Single();
            return menu;
        }
    }
}
